package cn.tedu.learn.tea.domain.service.impl;

import cn.tedu.learn.tea.domain.common.exception.BusinessException;
import cn.tedu.learn.tea.domain.common.util.FileUtil;
import cn.tedu.learn.tea.domain.response.StatusCode;
import cn.tedu.learn.tea.domain.service.PictureService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图片Service实现类
 */
@Service
public class PictureServiceImpl implements PictureService {

    @Value("${imagePath}")
    private String imagePath;

    @Value("${url}")
    private String url;

    @Override
    public String upload(MultipartFile file) {
        String urlPath = "";
        try {
            String fileName = FileUtil.makeTempFile(file, imagePath);
            urlPath = url + fileName;
            return urlPath;
        } catch (Exception e) {
            throw new BusinessException(StatusCode.FILE_UPLOAD_FAIL);
        }
    }
}
